/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *  
 * @author: jin Chen 
 * @date: Nov 30, 2004$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.control.gui;

import edu.psu.geovista.app.pcp.Pcp;
import edu.psu.geovista.app.pcp.plot.control.AxisLocationCfg;
import edu.psu.geovista.app.pcp.plot.control.ConfigManager;
import edu.psu.geovista.app.pcp.plot.control.Settings;
import edu.psu.geovista.app.pcp.plot.control.tools.AxisPlaceTool;
import edu.psu.geovista.app.pcp.plot.control.tools.BasicTool;
import edu.psu.geovista.app.pcp.toolkits.gui.EvenRowSectionsPane;
import edu.psu.geovista.app.plot.axis.YAxis;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

public class PlotStrechSection extends EvenRowSectionsPane
        implements ComponentListener{
    /**
     *  
     *  
     */
    private boolean updateSizeOnDrag;//=false;
    private boolean fillVertically;//=true;
    private boolean fillHorizontally;//=false;
   private javax.swing.JCheckBox fillVert;
   private javax.swing.JCheckBox updateOnDrag;
   private javax.swing.JCheckBox fillHorizon;
   private Pcp pcp;
   private ConfigManager cfgMgr;



    public PlotStrechSection(ConfigManager cfg) {
        this.cfgMgr=cfg;
        if(cfg!=null&&pcp!=null)  {
            pcp=cfg.getTargetUI().getPlot();
            ((JComponent)pcp).addComponentListener(this);
        }
        this.setLayout(3,1);
         this.mainP.setBorder(null);
        initComponents();
        this.addToMainPane(updateOnDrag);
        this.addToMainPane(fillVert);
        this.addToMainPane(fillHorizon);
        this.clearControlPane();
        reset();
    }

    public void reset(){
        if(cfgMgr!=null){
       boolean updateSizeOnDrag=((Boolean)cfgMgr.getConfigSetting(Settings.APPR_AUTOFIT)).booleanValue() ;
       boolean fillVertically=((Boolean)cfgMgr.getConfigSetting(Settings.APPR_FITV)).booleanValue() ;
       boolean fillHorizontally=((Boolean)cfgMgr.getConfigSetting(Settings.APPR_FITH)).booleanValue() ;
            this.setFillHorizontally(fillHorizontally);     //        fillHorizontally
            this.setFillVertically(fillVertically);
        }

       this.updateOnDrag.setSelected(updateSizeOnDrag);
       //this.fillVert.setSelected(fillVertically);
       //this.fillHorizon.setSelected(fillHorizontally);


    }
     public void setFillVertically(boolean b) {
         this.fillVertically=b;
         this.fillVert.setSelected(b);
         if (cfgMgr!=null) {
             AxisPlaceTool apCmd=(AxisPlaceTool) this.cfgMgr.getCmd(BasicTool.AxisPlaceT );
             if(apCmd!=null) apCmd.setFillVertically(fillVertically);
         }

     }

    public void setFillHorizontally(boolean b){
        this.fillHorizon.setSelected(b);
        this.fillHorizontally=b;
        if (cfgMgr!=null) {
            AxisPlaceTool apCmd=(AxisPlaceTool) this.cfgMgr.getCmd(BasicTool.AxisPlaceT );
            if(apCmd!=null) apCmd.setFillHorizontally(fillHorizontally);
        }
    }

     private void proposePlotSize(float ppw, float pph){
        AxisPlaceTool apCmd=(AxisPlaceTool) this.cfgMgr.getCmd(BasicTool.AxisPlaceT );
        apCmd.proposeChangePlotSize((int)ppw,(int)pph, true);



    }
    public void componentResized(ComponentEvent e) {
        if (/*this.updateSizeOnDrag*/this.updateOnDrag.isSelected() ){
            Component c=e.getComponent();
            if(c instanceof Pcp){
                Pcp pcp=(Pcp) c;
                pcp.fitwindow();
            }
        }


        AxisLocationCfg locationCfg = (AxisLocationCfg) cfgMgr.getCfg(ConfigManager.CFG_LOCATION);
        AxisSizeSection axisSizeP = locationCfg.getAxisSizeP();
        Pcp pcp = cfgMgr.getTargetUI().getPlot();
        edu.psu.geovista.app.plot.axis.XAxisOrder xaxis = pcp.getXaxis();
        int interval = xaxis.getInterval(1);

        YAxis axis = pcp.getYaxisList().getAxis(1);
        int height = axis.getActiveSize();

        axisSizeP.setAxisHeight(height);
        axisSizeP.setAxisInterval(interval);
    }

    public void componentMoved(ComponentEvent e) {
    }

    public void componentShown(ComponentEvent e) {
        //not work
        Component c=e.getComponent();
       /* if(c instanceof PlotConfig){
            this.updateView() ;
        }*/

    }

    public void componentHidden(ComponentEvent e) {
    }
    private void fillHorizonActionPerformed(java.awt.event.ActionEvent evt) {
        // Add your handling code here:
        //this.fillHorizontally =this.fillHorizon.isSelected() ;
        /*AxisPlaceTool apCmd=(AxisPlaceTool) this.cfgMgr.getCmd(BasicTool.AxisPlaceT );
        apCmd.setFillHorizontally(this.fillHorizon.isSelected() );*/
        this.setFillHorizontally(fillHorizon.isSelected());
    }

    private void fillVertActionPerformed(java.awt.event.ActionEvent evt) {
        // Add your handling code here:
        //this.fillVertically =this.fillVert.isSelected() ;
       /* AxisPlaceTool apCmd=(AxisPlaceTool) this.cfgMgr.getCmd(BasicTool.AxisPlaceT );
        apCmd.setFillVertically(this.fillVert.isSelected() );*/
        this.setFillVertically(fillVert.isSelected() );

    }

   /* private void updateOnDragActionPerformed(java.awt.event.ActionEvent evt) {
        this.updateSizeOnDrag =this.updateOnDrag.isSelected() ;
        // Add your handling code here:
    }*/

    private void initComponents() {
       updateOnDrag = new javax.swing.JCheckBox();
        fillVert = new javax.swing.JCheckBox();
        fillHorizon = new javax.swing.JCheckBox();



        //updateOnDrag.setSelected(this.updateSizeOnDrag);
        updateOnDrag.setText("Auto fit upon dragging window");
        updateOnDrag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
               // updateOnDragActionPerformed(evt);
            }
        });


        fillVert.setSelected(true);
        fillVert.setText("Fit vertically");
        fillVert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fillVertActionPerformed(evt);
            }
        });

        fillHorizon.setText("Fit horizontally");
                fillHorizon.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        fillHorizonActionPerformed(evt);
                    }
                });



    }




}
